package benny_driving_ser.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import benny_driving_ser.ObjectBean.DriHistoryOrderBean;
import benny_driving_ser.ObjectBean.UserHistoryOrderBean;
import benny_driving_ser.ObjectBean.UserInfoBean;
import benny_driving_ser.ObjectBean.UserOrderBean;

/**
 * sql语句封装对象数组工具类
 * @author Administrator
 */
public class SQL2ObjectListUtil {
	
	
	/**
	 * 运行sql语句获得对应的对象数组工具类
	 * @param o 获得对象的类型
	 * @param sql sql语句
	 * @param map 对象属性和对应表中字段的对应键值
	 * @return
	 */
	public static List<?> sql2ObjectList(Object o,String sql,Map<String, String> map){
		
		
		List list;
		Object c;
		try {
			c = Class.forName(o.getClass().getName());
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}

		List<String> attributes = new ArrayList<String>();
		List<String> fields = new ArrayList<String>();
		Iterator it = map.entrySet().iterator(); 
		while (it.hasNext()) { 
		    Map.Entry entry = (Map.Entry) it.next(); 
		    attributes.add(entry.getKey().toString().trim()); 
		    fields.add(entry.getValue().toString().trim());
		} 
		String[] setMethodNames = new String[attributes.size()]; 
		String[] getMethodNames = new String[attributes.size()]; 
		for(int i = 0;i<attributes.size();i++){
			setMethodNames[i] = "set"+(Character.toUpperCase((attributes.get(i).charAt(0))))+(attributes.get(i).substring(1));
		}
		Method[] setMethods = new Method[attributes.size()];
		Connection con = DBUtil.getInstance().openConnection();
		list = new ArrayList<UserInfoBean>();
		
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			PreparedStatement pst = con.prepareStatement(sql);
			ResultSet rs = pst.executeQuery(sql);
		
			while (rs.next()) {
				c = Class.forName(o.getClass().getName()).newInstance();
				for(int i = 0;i<setMethodNames.length;i++){
					setMethods[i] = c.getClass().getDeclaredMethod(setMethodNames[i],String.class);
					//System.out.println(setMethods.toString());
					String value = rs.getString(fields.get(i)+"");
					setMethods[i].invoke(c,value);
				}
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}  catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
		catch (InstantiationException e) {
			e.printStackTrace();
		}
		
		DBUtil.getInstance().closeConnection(con);
		return list;
		
	}
	
	
	public static void main(String[] args) throws Exception{
		List list;
		List<String> sqls = new ArrayList<String>();
		
		HashMap<String,String> map = new HashMap<String,String>();
		int userId = 1010;
		int driId = 1010;
		
		
//		String sql = "select pj.dwmc,pj.sjdz,hy.xm,hy.sjh,hy.add1,hy.add2,hy.add3," +
//				  "hy.add4,hy.add5,hy.platenum1,hy.platenum2,hy.platenum3,hy.platenum4" +
//				  ",hy.qsxm,hy.qsdh from t_hy hy,t_pj pj where hy.ID= "+userId+"  and hy.ID=pj.userId";
//		UserInfoBean userInfo =  new UserInfoBean();
//		map.put("name", "xm");
//		map.put("add1", "add1");
//		map.put("add2", "add2");
//		
//		
////		map.put("nameObject", "nameTable");
////		map.put("sexObject", "sexTable");
////		map.put("ageObject", "ageTable");
//		list =  sql2ObjectList(userInfo,sql,map);

		
		
//		//测试2
//		String sql = "select id,yyrq,gk,gksj,qd,zd,jl,jg,state,dri_id "
//				+ "from t_djpd where user_id = "+ userId + " AND state<7";
//		map.put("formId", "id");
//		map.put("time", "yyrq");
//		map.put("userName", "gk");
//		map.put("startPos", "qd");
//		map.put("desPos", "zd");
//		UserOrderBean userOrder = new UserOrderBean();
//		list = sql2ObjectList(userOrder,sql,map);
//		
		
		
//		//测试3 loadDriHistoryOrder
//		
//		String sql = "select t.id,t.start_time,t.end_time,t.gk,t.gksj,u.xm,t.qd,"
//				+ "t.zd,t.sjqd,t.sjzd,t.total,t.pay,t.sjlc,t.jl,t.jg,t.ksje "
//				+ "from t_djpd t,t_user u where t.dri_id= " + driId + " and t.state>5 and t.dri_id = u.id";
//		//sqls.add(sql);
//		map.put("formId", "id");
//		map.put("startTime", "start_time");
//		map.put("endTime", "end_time");
//		map.put("userName", "gk");
//		map.put("driName", "xm");
//		map.put("startsPos", "qd");
//		map.put("desPos", "zd");
//		map.put("astartPos", "sjqd");
//		map.put("cost", "total");
//		map.put("costWay", "pay");
//		map.put("mil", "sjlc");
//		map.put("exMil", "jl");
//		map.put("exCost", "jg");
//		DriHistoryOrderBean driInfo = new DriHistoryOrderBean();
//		list = sql2ObjectList(driInfo,sql,map);
		
		
//		//测试4  loadUserHistoryBean
//		UserHistoryOrderBean userOrder = new UserHistoryOrderBean();
//		String sql = "select t.id,t.gk,t.gksj,t.sowt,u.xm,u.dh,"
//				+ "u.username,t.qd,t.zd,t.start_time,t.end_time,t.total,"
//				+ "t.pay,t.sjlc ,t.isc from t_djpd t,t_user u " +
//				"where t.user_id = " + userId+ " AND u.id = t.dri_id AND t.state = 7";
//		map.put("userName", "gk");
//		map.put("formId", "id");
//		map.put("cost","total");
//		map.put("costWay","pay");
//		map.put("desPos","zd");
//		map.put("driName","xm");
//		list = sql2ObjectList(userOrder, sql, map);
		
		
//		System.out.println(list.toString());
	}
}
